/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

import com.bayu.common.lib.CommonUtil;
import com.bayu.common.lib.RoundNumber;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import com.bayu.controller.MainController;
import com.bayu.services.dao.PfDao;
import com.bayu.services.entity.Pf;
import java.awt.Color;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Bayu
 */
@Controller
public class PfGraph {

    private String query;
    PfDao pfDao;

    @Autowired
    private void setPfDao(PfDao pfDao){
      this.pfDao = pfDao;
    }
    @RequestMapping("/pf-graph.png")
    private void pfGraph (OutputStream stream) throws Exception{

        List maxValue= pfDao.custom("select max(pcsday) from Pf where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        List maxValue2 = pfDao.custom("select max(pcscumm) from Pf where date between '"+MainController.startDate+"' and '"+MainController.endDate+"'");
        String strMax2 = maxValue2.get(0).toString();
        String strMax = maxValue.get(0).toString();
        System.out.println(strMax2+"-"+strMax);

        double max1 = Double.valueOf(strMax).doubleValue()+1;
        double max2 = Double.valueOf(strMax2).doubleValue()+1;

        double maxtemp;
        if(max1 > max2){
            maxtemp = max1;
        }else{
            maxtemp = max2;
        }

        double min = 0.0;
        double max = maxtemp+1;
        JFreeChart chart = getPfGraph(min, max);
        ChartUtilities.writeChartAsPNG(stream, chart,1480,800);
    }
    private JFreeChart getPfGraph(double min,double max){
       DefaultCategoryDataset dataset = dataset();
       //String title="TRANSISI TOTAL RIJEKSI PIPE FITTING (INSPEKSI CASTING)";
        String tgl1="";
      String tgl2="";
      String title = "REJECTION CASTING INSPECTION PIPE FITTING TOTAL";

      if(!MainController.startDate.equals("")){
          tgl1 = CommonUtil.formatStringDate(MainController.startDate);
          tgl2 = CommonUtil.formatStringDate(MainController.endDate);
          title = title+"\r\n"+ tgl1+"-"+tgl2;
      }
       final JFreeChart chart = ChartFactory.createLineChart(
            title,        // chart title
           "Date",               // domain axis label
           "% Reject",                  // range axis label
            dataset,                 // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips?
            false                     // URL generator?  Not required...
        );
        chart.setBackgroundPaint(new Color(255, 255, 255));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer1 = plot.getRenderer();
        renderer1.setSeriesPaint(0, new Color(153,51,102));
        renderer1.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer1.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer1.setBaseItemLabelsVisible(true);
        NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
        rangeAxis.setAutoRange(false);
        rangeAxis.setRange(min,max);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

       return chart;
    }


    public DefaultCategoryDataset dataset(){
        String name1="% Pcs Day";
        String name2 = "% Pcs Cumm";
        DefaultCategoryDataset data1 = new DefaultCategoryDataset();
        List data = this.pfDao.getAllByDate(MainController.startDate, MainController.endDate);
        
        for(int i=0;i<data.size();i++){
          Pf c = (Pf)data.get(i);
          String cat[] = c.getDate().toString().split("-");
          data1.addValue(c.getPcsday(), name1, cat[2]);
          data1.addValue(c.getPcscumm(), name2, cat[2]);

        }

        return data1;
    }




}
